package com.ccl.iot.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.ccl.iot.R;
import com.ccl.iot.dvr.utils.LoggerUtil;
import com.ccl.iot.publics.UIUtils;

public class ColorPickerView extends View {
	
	private static final String TAG = "ColorPickerView";
	private Paint mPaint; // 取色板图片的画笔
	private Paint mCenterPaint; // 取色板的画笔

	private Paint mPickerPaint; //
	private int tmp = UIUtils.dp2px(10);
	private int mSelectedPointX = 0;
	private int mSelectedPointY = 0;

	private int[] mColors; // 取色板中的颜色值数组
	private Bitmap mColorBmp; // 取色板中的位图

	private boolean mIsTouchUp = false;
	private boolean mIsTouchDown = false;
	private Context mContext;

	// 取色板的长宽
	private static final int WIDTH = UIUtils.dp2px(300);
	private static final int HEIGHT = UIUtils.dp2px(300);
	private Bitmap mColorSelectorBitmap;

	private int mHeight;

	private int mWidth;
	private int mSelectorHeight;
	private int mSelectorWidth;
	private int top;

	public ColorPickerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		init();
	}

	public ColorPickerView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ColorPickerView(Context context) {
		this(context, null);
	}


	private void init() {
		// 设置取色板中的颜色值集合
		// mColors = new int[] { 0xFFFF0000, 0xFFBF4040, 0xFFCF9F70, 0xFFFF8000, 0xFFBFBF40, 0xFFFFFF00, 0xFF80BF40, 0xFF80FF00, 0xFF008800, 0xFF40BF40, 0xFF40BF40, 0xFF40BF80, 0xFF00FF80, 0xFF40BFBF,
		// 0xFF00FFFF, 0xFF4080BF, 0xFF0080FF, 0xFF4040BF, 0xFF0000FF, 0xFF000088};

//		mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
		mColors = new int[] { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00, 0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 };

		// 创建线性渐变着色器
		Shader shader = new LinearGradient(0, 0, WIDTH, 0, mColors, null, TileMode.REPEAT);

		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// 设置着色器
		mPaint.setShader(shader);
		// 设置画笔风格
		mPaint.setStyle(Paint.Style.FILL);
		// 创建取色板的图片
		mColorBmp = Bitmap.createBitmap(WIDTH, HEIGHT, Config.ARGB_4444);

		Canvas canvas = new Canvas(mColorBmp);

		// 将颜色值集合中的颜色画到图片上
		canvas.drawRect(new RectF(0, 0, WIDTH, HEIGHT), mPaint);

		mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		mColorSelectorBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon_color_selector);

		mSelectorHeight = mColorSelectorBitmap.getHeight();
		mSelectorWidth = mColorSelectorBitmap.getWidth();

	}


	private boolean flag;
	@Override
	protected void onDraw(Canvas canvas) {
		// 将图片画到取色板
		canvas.drawBitmap(mColorBmp, 0, 0, mCenterPaint);

		Rect srcRect = new Rect(0,0,mSelectorWidth,mSelectorHeight);
//		LoggerUtil.i(TAG,"srcRect:" + srcRect);

		Rect dstRect = new Rect(mSelectedPointX,0,mSelectedPointX + mSelectorWidth,this.getBottom());
//		LoggerUtil.i(TAG,"dstRect:" + dstRect);

		canvas.drawBitmap(mColorSelectorBitmap,srcRect,dstRect,mCenterPaint);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (widthMode == MeasureSpec.EXACTLY) {
			mWidth = width;
		} else {
			mWidth = WIDTH;
		}
		if (heightMode == MeasureSpec.EXACTLY) {
			mHeight = height;
		} else {
			mHeight = HEIGHT;
		}

		top = this.getTop();

		setMeasuredDimension(mWidth, mHeight);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int x = (int) event.getX();
		int y = (int) event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mIsTouchDown = true;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			mIsTouchUp = true;
			break;
		}

		// 限制触摸的范围
		x = x < 0 ? 0 : x;
		int width = mWidth - mColorSelectorBitmap.getWidth();
		x = x >= width ? width : x;
		y = y < 0 ? 0 : y;
		y = y >= HEIGHT ? HEIGHT - 1 : y;

		LoggerUtil.i(TAG,"HEIGHT：" + HEIGHT + "--WIDTH:" + WIDTH + "--实际高度：" + mHeight + "--实际宽度："+ mWidth);

		// 获取像素的argb值
		int pixel = mColorBmp.getPixel(x, y);
//		LoggerUtil.e(TAG,"像素的argb值:" + pixel);
		mSelectedPointX = x;
		mSelectedPointY = y;
		if (mListener != null) {
			mListener.onColorChange(pixel, mIsTouchUp, mIsTouchDown);
			mIsTouchUp = false;
			mIsTouchDown = false;
		}

		invalidate();
		return true;
	}

	public interface OnColorChangeListener {
		public void onColorChange(int color, boolean isTouchUp, boolean isTouchDown);
	}

	private OnColorChangeListener mListener = null;

	public void setOnColorChangeListener(OnColorChangeListener l) {
		mListener = l;
	}

}
